package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.etl.model.EtlInput;
import com.jorado.etl.repository.RdPositionRepository;
import com.jorado.event.EventClient;
import com.jorado.event.dubbo.util.SpringUtils;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import org.springframework.context.annotation.Description;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

@Description("rd数据库语料抽取服务")
public final class RdDBEtlService extends ExportToFileImpl<Map<String, Object>> {

    private int total;
    private int waitTime;
    private EtlService etlService;

    public RdDBEtlService(String distFile) {
        this(distFile, true, 0);
    }

    public RdDBEtlService(String distFile, boolean deleteIfExists, int waitTime) {
        super(SpringUtils.getBean(RdPositionRepository.class), distFile, deleteIfExists);
        this.waitTime = waitTime;
        this.etlService = new EtlService(this.distFile);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<Map<String, Object>> dataList) {

        total += dataList.size();

        HashSet<EtlInput> etlList = new LinkedHashSet<>();

        for (Map<String, Object> row : dataList) {
            System.err.println(row);
        }

        this.etlService.save(etlList);

        try {
            Thread.sleep(waitTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取rd数据库职位:[%d]个", total));

        return Result.OK;
    }
}
